<template>
	<view class="login">
		<image class="logoImg" src="@/static/imgs/jyuLogo2.png" mode="aspectFill"></image>
		<view class="title">
			欢迎使用嘉应学院团委小程序
		</view>
		<button @click='loginWx'>一键登录</button>
	</view>
</template>

<script setup>
	import {
		wxLoginB
	} from '@/apis/apis.js'
	import {
		useUserStore
	} from '@/store'
	import {
		ref
	} from 'vue'
	const useUser = useUserStore()


	const loginTemp = async (code) => {
		const resTemp = await wxLoginB({
			code: code
		})
		uni.setStorageSync('jyu_token', resTemp.token);
		useUser.userInf = JSON.parse(JSON.stringify(resTemp.user))
		useUser.token = resTemp.token
		
		console.log();
		if (resTemp.token) {
			if (resTemp.new) {
				uni.showToast({
					title: '欢迎加入嘉应学院团委小程序!',
					duration: 2000,
				})
			} else if (resTemp.new === false) {
				uni.showToast({
					title: '登录成功!',
					duration: 2000,
				})
			}
			setTimeout(() => {
				uni.navigateBack()
			}, 2000)
		} else {
			uni.showToast({
				title: '登录失败',
				duration: 2000,
			})
		}
		return resTemp
	}
	const loginWx = () => {
		wx.login({
			success(res) {
				if (res.code) {
					const resTemp = loginTemp(res.code)
				} else {
					console.log('登录失败！' + res.errMsg)
				}
			}
		})
	}
</script>

<style lang="less" scoped>
	.login {
		width: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		gap: 30rpx;

		.logoImg {
			margin-top: 250rpx;
			height: 500rpx;
			width: 500rpx;
		}

		.title {
			font-size: 50rpx;
		}
	}
</style>